package com.hnkywl.sts.action.jggl;

import com.hnkywl.sts.entity.jggl.Jgd;
import com.hnkywl.sts.entity.jggl.Jgdmx;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.service.jggl.JgdService;
import com.hnkywl.sts.service.xsgl.KhService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by Melody on 14-3-12.
 */
@Namespace("/jggl/jgdgl")
public class JgdAction extends SimpleActionSupport {

    private JgdService jgdService;
    private KhService khService;

    private String jgdh;
    private Date sqrqStart;
    private Date sqrqEnd;
    private Long sqgsId;
    private Long sbgsId;
    private Integer zt;
    private int page;
    private int rows;
    private Date currentTime = new Date();
    private Long jgdId;
    private String jsonText;
    private String jsonText1;
    private Jgd jgd;
    private String mxzt;
    private String rate;
    private String bz;
    private Long jgdmx_id;
    private String ckdh;
    private String khbm;
    private Integer sftzd;
    private Double cxsl;
    private Double cxzl;
    private Long ckdId;
    private String cxdh;


    @Action("jgdList")
    public void jgdList() throws ParseException {
        this.renderJson(this.jgdService.findJgdList(jgdh, sqrqStart, sqrqEnd, sqgsId, sbgsId, zt, page, rows));
    }

    // 点击新增加工单
    @Action("jgd_into")
    public String jgd_into() {
        return SUCCESS;
    }

    // 新增
    @Action("jgd_add")
    public void jgd_add() {
        try {
            JSONArray array = new JSONArray(jsonText);
            Jgd jgd = jgdService.addJgd(array);
            if (null == jgd) {
                renderText("{\"statusCode\":\"300\", \"message\":\"传入数据异常，请检查数据格式！\", \"operationType\":\"add\"}");
            } else {
                renderText("{\"statusCode\":\"200\", \"message\":\"添加加工单成功！\", \"operationType\":\"add\", \"jgdId\":" + jgd.getId() + "}");
            }
        } catch (Exception e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"新增数据异常！\", \"operationType\":\"add\"}");
        }

    }

    // 加工单详细
    @Actions({@Action("jgd_detail"), @Action("jgd_content")})
    public String jgd_detail() {
        jgd = jgdService.findById(jgdId);
        return SUCCESS;
    }

    // 查看，加工单明细
    @Action("jgddetail_wzmx")
    public void jgddetail_wzmx() {
        this.renderJson(this.jgdService.findJgdmxDetail(mxzt, jgdId, page, rows));
    }


    // 查看，加工单预期成材明细
    @Action("jgddetail_yqccmx")
    public void jgddetail_yqccmx() {
        this.renderJson(this.jgdService.jgddetail_yqccmx(jgdId, page, rows));
    }

    //加工调入冲销明细列表
    @Action("rkdList")
    public void rkdList() {
        this.renderJson(this.jgdService.findJgdmxDrCxDetail(jgdh, khbm, sftzd, page, rows));
    }

    /**
     * 方法名:     deleteJgd
     * 功能描述:      删除加工单
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("delete_jgd")
    public void deleteJgd() {
        try {
            this.jgdService.deleteJgd(jgdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除加工单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 审批加工单
    @Action("change_status")
    public void changeStatus() {
        try {
            // 判断可开单量是否充足
            boolean bool = true;
            String khbm = "";
            Jgd jgd = this.jgdService.findJgdDetail(jgdId);
            List<Jgdmx> jgdmxList = jgd.getJgdmxList();
            for (Jgdmx jgdmx : jgdmxList) {
                Kh kh = this.khService.findKh(jgdmx.getKh().getId());
                if (jgdmx.getSbsl() > kh.getKkdsl()) {
                    bool = false;
                    khbm = kh.getKhbm();
                    break;
                }
            }
            if (bool) {
                try {
                    // 发送到仓储系统
                    JSONObject json = new JSONObject(this.jgdService.createJgdProcessToJson(jgd));
                    if (null != json && StringUtils.isNotBlank(json.toString())) {
                        if (null == json.get("code") || StringUtils.isBlank(json.get("code").toString())) {
                            renderText("{\"statusCode\":\"300\", \"message\":\"" + "调用接口错误：" + json + "\"}");
                        } else {
                            if (StringUtils.equals("0", json.get("code").toString())) {
                                // 改变状态
                                this.jgdService.changeStatus(jgdId, zt);
                                // 减去库存
                                for (Jgdmx jgdmx : jgdmxList) {
                                    Kh kh = this.khService.findKh(jgdmx.getKh().getId());
                                    kh.setKkdsl(kh.getKkdsl() - jgdmx.getSbzl());
//                                    kh.setJs(kh.getJs() - (int)((double)jgdmx.getSbsl()));
                                    khService.save(kh);
                                }
                                renderText("{\"statusCode\":\"200\", \"message\":\"审批加工单成功！\"}");
                            } else {
                                renderText("{\"statusCode\":\"300\", \"message\":\"" + "调用接口返回结果错误信息为：" + json.get("message").toString() + "\"}");
                            }
                        }

                    } else {
                        renderText("{\"statusCode\":\"300\", \"message\":\"" + "调用接口返回结果错误，接口为空！\"}");
                    }
                } catch (Exception e) {
                    renderText("{\"statusCode\":\"300\", \"message\":\"" + "调用接口错误:" + e.getMessage() + "\"}");
                }
            } else {
                renderText("{\"statusCode\":\"300\", \"message\":\"加工单提交失败，该卡号:" + khbm + ",申报数量超出库存可开单数量，请修改申报数量！\"}");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 修改，加工单明细
    @Action("jggl_jgdmx")
    public void jggl_jgdmx() {
        this.renderJson(this.jgdService.findJgdmxContent(jgdId));
    }

    // 修改，加工单预期成材明细
    @Action("jggl_yqccmx")
    public void jggl_yqccmx() {
        renderJson(this.jgdService.findJgglYqccmx(jgdId));
    }

    // 修改
    @Action("update_jgd")
    public void update_jgd() {
        try {
            JSONArray array = new JSONArray(jsonText);
            JSONArray array1 = new JSONArray(jsonText1);
            Jgd jgd = jgdService.updateJgd(array, array1, rate, bz);
            if (null == jgd) {
                renderText("{\"statusCode\":\"300\", \"message\":\"传入数据异常，请检查数据格式！\", \"operationType\":\"add\"}");
            } else {
                renderText("{\"statusCode\":\"200\", \"message\":\"修改加工单成功！\", \"operationType\":\"add\"}");
            }
        } catch (Exception e) {
            renderText("{\"statusCode\":\"300\", \"message\":\"修改数据异常！\", \"operationType\":\"add\"}");
        }

    }

    // 出库单返回明细
    @Action("ckd_detail")
    public String ckd_detail() {
        return SUCCESS;
    }

    // 出库单明细信息
    @Action("ckdDetail_Wzmx")
    public void ckdDetail_Wzmx() {
        this.renderJson(this.jgdService.findCkdDetail(jgdmx_id, page, rows));
    }

    // 冲销出库单list
    @Action("ckdcxList")
    public void ckdcxList() throws ParseException {
        this.renderJson(this.jgdService.findCkdcxList(ckdh, khbm, sftzd, cxdh, page, rows));
    }

    // 冲销审批
    @Action("chang_ckdcx")
    public void chang_ckdcx() {
        try {
            // 发送到仓储系统
            if (null != jgdmx_id && null != cxsl && null != cxzl) {
                Jgdmx jgdmx = jgdService.findJgdmxById(jgdmx_id);
                if (null != jgdmx) {
                    Kh kh = this.khService.findKh(jgdmx.getKh().getId());
                    if (null != kh) {
                        kh.setSl(kh.getSl() + Math.abs(cxzl));
                        kh.setKkdsl(kh.getKkdsl() + Math.abs(cxzl));
                        kh.setJs(kh.getJs() + (int) ((double) Math.abs(cxsl)));
                        khService.save(kh);

                        jgdmx.setJg_sbsl(jgdmx.getJg_sbsl() - Math.abs(cxsl));
                        jgdmx.setJg_sbzl(jgdmx.getJg_sbzl() - Math.abs(cxzl));
                        jgdService.saveJgdmx(jgdmx, ckdId);

                        renderText("{\"statusCode\":\"200\", \"message\":\"加工单冲销审核成功！\"}");
                    } else {
                        renderText("{\"statusCode\":\"300\", \"message\":\"" + "加工单明细信息没有关联到卡号！\"}");
                    }
                } else {
                    renderText("{\"statusCode\":\"300\", \"message\":\"" + "没有查询到加工单明细信息！\"}");
                }
            } else {
                renderText("{\"statusCode\":\"300\", \"message\":\"" + "传入的数据异常！\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("chang_rkdcx")
    public void chang_rkdcx() {
        try {
            // 发送到仓储系统
            if (null != jgdmx_id && null != cxsl && null != cxzl) {
                Jgdmx jgdmx = jgdService.findJgdmxById(jgdmx_id);
                if (null != jgdmx) {
                    Kh kh = this.khService.findKh(jgdmx.getKh().getId());
                    if (null != kh) {
                        jgdService.handJgdDrCxKh(kh);
                        jgdmx.setSftzd(true);
                        jgdService.saveJgdmx(jgdmx);
                        renderText("{\"statusCode\":\"200\", \"message\":\"加工入库单单冲销审核成功！\"}");
                    } else {
                        renderText("{\"statusCode\":\"300\", \"message\":\"" + "加工单明细信息没有关联到卡号！\"}");
                    }
                } else {
                    renderText("{\"statusCode\":\"300\", \"message\":\"" + "没有查询到加工单明细信息！\"}");
                }
            } else {
                renderText("{\"statusCode\":\"300\", \"message\":\"" + "传入的数据异常！\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Autowired
    public void setJgdService(JgdService jgdService) {
        this.jgdService = jgdService;
    }

    @Autowired
    public void setKhService(KhService khService) {
        this.khService = khService;
    }

    public String getJgdh() {
        return jgdh;
    }

    public void setJgdh(String jgdh) {
        this.jgdh = jgdh;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public Long getSqgsId() {
        return sqgsId;
    }

    public void setSqgsId(Long sqgsId) {
        this.sqgsId = sqgsId;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Date getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public String getJsonText() {
        return jsonText;
    }

    public void setJsonText(String jsonText) {
        this.jsonText = jsonText;
    }

    public String getJsonText1() {
        return jsonText1;
    }

    public void setJsonText1(String jsonText1) {
        this.jsonText1 = jsonText1;
    }

    public Long getJgdId() {
        return jgdId;
    }

    public void setJgdId(Long jgdId) {
        this.jgdId = jgdId;
    }

    public Jgd getJgd() {
        return jgd;
    }

    public void setJgd(Jgd jgd) {
        this.jgd = jgd;
    }

    public String getMxzt() {
        return mxzt;
    }

    public void setMxzt(String mxzt) {
        this.mxzt = mxzt;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public Long getJgdmx_id() {
        return jgdmx_id;
    }

    public void setJgdmx_id(Long jgdmx_id) {
        this.jgdmx_id = jgdmx_id;
    }

    public Integer getSftzd() {
        return sftzd;
    }

    public void setSftzd(Integer sftzd) {
        this.sftzd = sftzd;
    }

    public String getCkdh() {
        return ckdh;
    }

    public void setCkdh(String ckdh) {
        this.ckdh = ckdh;
    }

    public String getKhbm() {
        return khbm;
    }

    public void setKhbm(String khbm) {
        this.khbm = khbm;
    }

    public Double getCxsl() {
        return cxsl;
    }

    public void setCxsl(Double cxsl) {
        this.cxsl = cxsl;
    }

    public Double getCxzl() {
        return cxzl;
    }

    public void setCxzl(Double cxzl) {
        this.cxzl = cxzl;
    }

    public Long getCkdId() {
        return ckdId;
    }

    public void setCkdId(Long ckdId) {
        this.ckdId = ckdId;
    }

    public String getCxdh() {
        return cxdh;
    }

    public void setCxdh(String cxdh) {
        this.cxdh = cxdh;
    }
}
